# -*-coding:utf-8-*-
from typing import TextIO, Text
from abc import ABCMeta, abstractmethod

# 需要将类中的方法的参数类型注解为类本身时，用引号+类名字
class my_str(object):
    def __init__(self, _str):
        self._str = _str

    def add(self, other: 'my_str') -> str:
        if isinstance(other, str):
            return self._str + '*' + other
        if isinstance(other, my_str):
            return self._str + '*' + other._str

class Converter(object, metaclass=ABCMeta):
    def __init__(self, reader: TextIO, writer: TextIO):
        self._reader = reader
        self._writer = writer

    def process(self) -> None:
        while True:
            text_source = self._reader.readline()
            if not text_source:
                break
            text_result = self.convert(text_source)
            self._writer.write(text_result)

    @abstractmethod
    def convert(self, data: Text) -> Text:
        pass

    def __enter__(self):
        print('enter')
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._reader.close()
        self._writer.close()
        print('exit')


class LowerConverter(Converter):
    def convert(self, data: Text) -> Text:
        return data.lower()

if __name__ == '__main__':
    with LowerConverter(open('course.txt', 'r'), open('a.txt', 'w')) as c:
        c.process()
